<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API测试页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .test-section {
            margin: 20px 0;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        button {
            background: #007acc;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background: #005a9e;
        }
        .result {
            margin-top: 10px;
            padding: 10px;
            background: #f5f5f5;
            border-radius: 3px;
            white-space: pre-wrap;
            font-family: monospace;
        }
        .error {
            background: #ffe6e6;
            color: #d00;
        }
        .success {
            background: #e6ffe6;
            color: #080;
        }
    </style>
</head>
<body>
    <h1>API测试页面</h1>
    
    <div class="test-section">
        <h2>测试1: 直接fetch请求</h2>
        <button onclick="testFetch()">测试 fetch API</button>
        <div id="fetch-result" class="result"></div>
    </div>
    
    <div class="test-section">
        <h2>测试2: XMLHttpRequest请求</h2>
        <button onclick="testXHR()">测试 XMLHttpRequest</button>
        <div id="xhr-result" class="result"></div>
    </div>
    
    <div class="test-section">
        <h2>测试3: 检查网络连接</h2>
        <button onclick="testConnection()">测试连接</button>
        <div id="connection-result" class="result"></div>
    </div>

    <script>
        const API_URL = 'http://localhost:8080/api/v1/items/93';
        
        async function testFetch() {
            const resultDiv = document.getElementById('fetch-result');
            resultDiv.textContent = '正在测试...';
            resultDiv.className = 'result';
            
            try {
                console.log('🚀 开始fetch测试:', API_URL);
                const response = await fetch(API_URL, {
                    method: 'GET',
                    headers: {
                        'Content-Type': 'application/json'
                    }
                });
                
                console.log('📡 fetch响应状态:', response.status);
                const data = await response.json();
                console.log('📦 fetch响应数据:', data);
                
                resultDiv.textContent = `状态: ${response.status}\n数据: ${JSON.stringify(data, null, 2)}`;
                resultDiv.className = 'result success';
            } catch (error) {
                console.error('❌ fetch错误:', error);
                resultDiv.textContent = `错误: ${error.message}`;
                resultDiv.className = 'result error';
            }
        }
        
        function testXHR() {
            const resultDiv = document.getElementById('xhr-result');
            resultDiv.textContent = '正在测试...';
            resultDiv.className = 'result';
            
            const xhr = new XMLHttpRequest();
            xhr.open('GET', API_URL, true);
            xhr.setRequestHeader('Content-Type', 'application/json');
            
            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        try {
                            const data = JSON.parse(xhr.responseText);
                            console.log('📦 XHR响应数据:', data);
                            resultDiv.textContent = `状态: ${xhr.status}\n数据: ${JSON.stringify(data, null, 2)}`;
                            resultDiv.className = 'result success';
                        } catch (e) {
                            resultDiv.textContent = `解析错误: ${e.message}\n原始响应: ${xhr.responseText}`;
                            resultDiv.className = 'result error';
                        }
                    } else {
                        console.error('❌ XHR错误状态:', xhr.status);
                        resultDiv.textContent = `HTTP错误: ${xhr.status}\n响应: ${xhr.responseText}`;
                        resultDiv.className = 'result error';
                    }
                }
            };
            
            xhr.onerror = function() {
                console.error('❌ XHR网络错误');
                resultDiv.textContent = '网络错误: 无法连接到服务器';
                resultDiv.className = 'result error';
            };
            
            xhr.send();
        }
        
        async function testConnection() {
            const resultDiv = document.getElementById('connection-result');
            resultDiv.textContent = '正在测试连接...';
            resultDiv.className = 'result';
            
            const tests = [
                { name: '后端健康检查', url: 'http://localhost:8080/health' },
                { name: '后端根路径', url: 'http://localhost:8080/' },
                { name: '目标API', url: API_URL }
            ];
            
            let results = [];
            
            for (const test of tests) {
                try {
                    const response = await fetch(test.url, { method: 'GET' });
                    results.push(`${test.name}: ✅ ${response.status}`);
                } catch (error) {
                    results.push(`${test.name}: ❌ ${error.message}`);
                }
            }
            
            resultDiv.textContent = results.join('\n');
            resultDiv.className = 'result';
        }
    </script>
</body>
</html>